<?php
require_once 'Sabai/Controller/ModelEntityList.php';

class Xigg_Admin_Category_List extends Sabai_Controller_ModelEntityList
{
    function Xigg_Admin_Category_List()
    {
        parent::Sabai_Controller_ModelEntityList('Category');
    }

    function &_getRequestedCriteria(&$request)
    {

        $criteria =& Sabai_Model_Criteria::createValue('category_parent', 'NULL');
        return $criteria;
    }

    function &_onListEntities(&$entities, &$context)
    {
        $select = $context->request->getAsStr('select', 'all');
        if ($select != 'top') {
            $select = 'all';
            $model =& $this->_locator->getService('Model');
            $category_r =& $model->getRepository('Category');
            $children = array();
            foreach ($entities->getAllIds() as $id) {
                $children[$id] =& $category_r->fetchDescendantsAsTreeByParent($id);
            }
            $entities->rewind();
            $context->response->setVar('child_categories', $children);
        }
        $context->response->setVars(array('requested_select' => $select));
        return $entities;
    }
}